package com.k8sops.backend.controller;

import com.k8sops.backend.model.entity.DOSReleaseYaml;
import com.k8sops.backend.model.response.ApiResult;
import com.k8sops.backend.service.DOSReleaseYamlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * YAML内容控制器
 */
@RestController
@RequestMapping("/release/yaml")
@Api(tags = "YAML内容管理")
@Slf4j
public class DOSReleaseYamlController {

    @Autowired
    private DOSReleaseYamlService dosReleaseYamlService;

    /**
     * 根据releaseId获取YAML内容
     *
     * @param releaseId 部署ID
     * @return YAML内容
     */
    @GetMapping("/content/{releaseId}")
    @ApiOperation("根据releaseId获取YAML内容")
    public ApiResult<String> getYamlContentByReleaseId(@PathVariable("releaseId") Integer releaseId) {
        try {
            DOSReleaseYaml yaml = dosReleaseYamlService.getLatestYamlByReleaseId(releaseId);
            if (yaml != null) {
                return ApiResult.success(yaml.getYamlContent());
            } else {
                return ApiResult.failed("未找到对应的YAML内容");
            }
        } catch (Exception e) {
            log.error("获取YAML内容失败", e);
            return ApiResult.failed("获取YAML内容失败: " + e.getMessage());
        }
    }
}
